$(document).ready(function() {
   var resumecontent = $("#resumecontent"); 

   var loadResume=function(source) {
      $.get(OC.filePath('resumes', 'ajax/search', 'getoffsiteresume.php') + '?resumecode='+ resumecontent.data('resumecode')+"&source="+source+"&_t="+resumecontent.data('added'), function(html) {
         var iframes = html.split('<div id="_ppp_iframe_delimiter"></div>'); 
         resumecontent.html('');
         $.each(iframes, function(i, html) {
            resumecontent.append("<iframe id='resumecontent"+i+"' frameborder='0' style='border:none; width:100%; overflow: hidden'></iframe>");
            var iframe = $('#resumecontent'+i, resumecontent); 
            var doc = iframe.get(0).contentWindow.document; 
            doc.open();
            doc.write(html);
            doc.close();     
            iframe.height($(doc).height()+(i==0 ? 50 : 100));
            if (source == "zhilian") {
               $(doc).find('table').attr('width', 768);
               $(doc).find('table').first().find("table").addBack().attr('width', 748);
            }
            iframe.one('load',function(){
               iframe.height($(doc).height()+(i==0 ? 50 : 100));
            });
            // reset the iframe height based on image loading completion 
            $(doc).find('img').filter(function(i) { return $(this).attr('src') && /data:image/.test($(this).attr('src'));}).each(function() {
               $(this).one('load', function() {
                  iframe.height($(doc).height()+10);
               }).each(function() {
                  /* if load from browser cache, we have complete property */
                  if(this.complete) $(this).load();
               });
            });
         })
      })
   };
   loadResume(resumecontent.data('source').split(',').shift());
   
   $('.selectedActions').on('click', '#import',function(){
      if ($(this).hasClass('imported')) {
         $('.import_confirm .confirmtext.import').hide();
         $('.import_confirm .confirmtext.imported').show();
      } else {
         $('.import_confirm .confirmtext.imported').hide();
         $('.import_confirm .confirmtext.import').show();
      }
      $('.import_confirm').modal();
      return false;
   });

   $('.import_confirm').on('click', '.ppp_bluebtn', function(event) {
      var source = $(".import_confirm").data('source');
      $('.import_confirm').modal("hide");

      var sync_type = resumecontent.data('source');
      var resumecode = [];
      resumecode.push(resumecontent.data('resumecode'));

      var data = {
         type: sync_type,
         syncaction: 'saveresumes',
         resumecode: JSON.stringify(resumecode)
      };

      startSync(sync_type, data);
      return false;
   });

});
